<template>
  <a-modal
    :title="model && model.id > 0 ? '新建通知' : '新建通知'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-alert
        type="error"
        message="消息一旦发送不可撤回，请谨慎操作！！！每个会员每月仅可收到4条消息，请珍惜发送次数，切勿滥用骚扰客户！！！！"
      />
      <a-form :form="form" v-bind="formLayout" style="margin-top: 24px;">
        <a-form-item label="选择标签">
          <a-select v-decorator="['type', { initialValue: '' }]">
            <a-select-option :value="i.id" v-for="i in option" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="通知内容">
          <a-textarea
            rows="4"
            placeholder="尽可能不要内容过长，避免发送失败"
            v-decorator="['memo', { rules: [{ required: true, message: '请输入' }] }]"
          />
        </a-form-item>

        <a-form-item label="发送确认">
          <a-input v-decorator="['phone', { initialValue: '' }]" placeholder="请输入“201610”六位数字" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import {getMemLabelListUsingPOST} from '@/api/api/xitongguanli.ts'
// 表单字段
const fields = ['description', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      option:[],
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    this.getOption()
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods:{
    getOption(){
      getMemLabelListUsingPOST(this.optionParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  }
}
</script>
